package com.idopartx.phonelightning.utils

import com.idopartx.phonelightning.entity.LightNightVo

object MorseUtils {


    /**
     * 摩尔斯对照表
     * @author小奎
     */
    fun getMorseList(): HashMap<Char, String> {
        // 摩尔斯编码表集合
        val mapList = HashMap<Char, String>()
        mapList.put('A', "·-")
        mapList.put('B', "-···")
        mapList.put('C', "-·-·")
        mapList.put('D', "-··")
        mapList.put('E', "·")
        mapList.put('F', "··-·")
        mapList.put('G', "--·")
        mapList.put('H', "····")
        mapList.put('I', "··")
        mapList.put('G', "·---")
        mapList.put('K', "-·-")
        mapList.put('L', "·-··")
        mapList.put('M', "--")
        mapList.put('N', "-·")
        mapList.put('O', "---")
        mapList.put('P', "·--·")
        mapList.put('Q', "--·-")
        mapList.put('R', "·-·")
        mapList.put('S', "···")
        mapList.put('T', "-")
        mapList.put('U', "··-")
        mapList.put('V', "···-")
        mapList.put('W', "·--")
        mapList.put('X', "-··-")
        mapList.put('Y', "-·--")
        mapList.put('Z', "--··")

        /* 数字电码0-9 */
        mapList.put('0', "-----")
        mapList.put('1', "·----")
        mapList.put('2', "··---")
        mapList.put('3', "···--")
        mapList.put('4', "····-")
        mapList.put('5', "·····")
        mapList.put('6', "-····")
        mapList.put('7', "--···")
        mapList.put('8', "---··")
        mapList.put('9', "----·")

        /* 标点符号，可自增删 */
        mapList.put(',', "--··--") // ,逗号
        mapList.put('.', "·-·-·-") // .句号
        mapList.put('?', "··--··") // ?问号
        mapList.put('!', "-·-·--") // !感叹号
        mapList.put('\'', "·----·")// '单引号
        mapList.put('\"', "·-··-·")// "引号
        mapList.put('=', "-···-")    // =等号
        mapList.put(':', "---···") // :冒号
        mapList.put(';', "-·-·-·") // 分号
        mapList.put('(', "-·--·")    // (前括号
        mapList.put(')', "-·--·-") // )后括号
        mapList.put(' ', "★")
        return mapList
    }

    fun trans2Morse(str: String?): String {
        var transTxt = ""
        str?.forEach {
            transTxt += getMorseList()[it.uppercaseChar()]
        }
        return transTxt
    }

    fun trans2Lightning(morseStr: String?): MutableList<LightNightVo> {

        val morseTransList = arrayListOf<LightNightVo>()

        morseStr?.uppercase()?.forEach {
            when (it.toString()) {
                "-" -> {
                    morseTransList.add(LightNightVo(300, 200))
                }

                "·" -> {
                    morseTransList.add(LightNightVo(100, 200))
                }

                else -> {

                }
            }
        }
        return morseTransList
    }

    fun getMorseTime(morseStr: String): String {
        var morseTime: Long = 0
        morseStr.forEach {
            when (it.toString()) {
                "-" -> {
                    morseTime += 500
                }

                "·" -> {
                    morseTime += 300
                }

                else -> {

                }
            }
        }
        return "${morseTime / 1000}s"
    }


}